/*
 * cuboid.cpp
 *
 *  Created on: 19.12.2008
 *      Author: felix
 */
#ifdef WIN32
#include <windows.h>
#endif

#ifdef __APPLE__
#include <OpenGL/gl.h>  // The GL Header File
#include <GLUT/glut.h>  // The GL Utility Toolkit (Glut) Header
#else
#include <GL/gl.h>      // The GL Header File
#include <GL/glut.h>    // The GL Utility Toolkit (Glut) Header
#include <GL/glext.h>
#endif

#include "cuboid.h"
#ifndef CUBE_H_
#include "cube.h"
#endif

Cuboid::Cuboid(int length, int width, int depth, Color color, bool isAnalyph) {
	this->x = 0; this->y = 0; this->z = 0;
	this->length = length; this->width = width; this->depth = depth;
	this->color = color;
	this->isAnaglyph = isAnalyph;


	setSize(length, width, depth);
}

Cuboid::Cuboid() {
	// empty constructor
}

Cuboid::Cuboid(const Cuboid& c) {

	this->x = c.x; this->y = c.y; this->z = c.z;
	this->length = c.length; this->width = c.width; this->depth = c.depth;
	this->color.r = c.color.r;
	this->color.g = c.color.g;
	this->color.b = c.color.b;
	this->setSize(this->length,this->width,this->depth);
	this->isAnaglyph = c.isAnaglyph;
}

Cuboid& Cuboid::operator=(const Cuboid& c) {

	if (this != &c) {
		delete &color;
		this->x = c.x; this->y = c.y; this->z = c.z;
		this->length = c.length; this->width = c.width; this->depth = c.depth;
		this->color.r = c.color.r;
		this->color.g = c.color.g;
		this->color.b = c.color.b;
		this->setSize(this->length,this->width,this->depth);
		this->isAnaglyph = c.isAnaglyph;
	}
	return *this;
}

void Cuboid::setSize(int length, int width, int depth) {

	cubes.clear();
	this->length = length; this->width = width; this->depth = depth;
	for (int i=0; i < length; i++) {
		for (int j=0; j < width; j++) {
			for (int k=0; k < depth; k++) {
				//j-width so its drawn "on" the carpet , not underneeth
				Cube c(i,j-width,k);
				cubes.push_back(c);
			}
		}
	}
}

void Cuboid::draw(GLUquadric *quadric) {
	drawPreview(this->x, this->y, this->z, quadric); // set the preview brick
}

void Cuboid::drawPreview(int x, int y, int z, GLUquadric *quadric) {
	glPushMatrix();
	glTranslatef(x, y, z);

	if (!isAnaglyph) {
		 glColor3f(color.r, color.g, color.b);
	}

	for (unsigned int i=0; i < cubes.size(); i++) {
		cubes.at(i).draw(quadric);
	}

	glPopMatrix();
}

